package com.slipper.websocket;

import com.slipper.core.message.Subscriber;
import org.springframework.web.socket.WebSocketSession;

public class Websocket {
    private String id;
    private WebSocketSession session;
    private volatile Subscriber subscriber;

    public Websocket(WebSocketSession session) {
        this.id = session.getId();
        this.session = session;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public WebSocketSession getSession() {
        return session;
    }

    public void setSession(WebSocketSession session) {
        this.session = session;
    }

    public Subscriber computeIfAbsent() {
        if (subscriber == null) {
            synchronized(this) {
                if (subscriber == null) {
                    subscriber = new WebsocketSubscriber(this.session);
                }
            }
        }
        return subscriber;
    }
}
